/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package dao;

import helper.NewHibernateUtil;
import java.util.Date;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import pojo.*;

/**
 *
 * @author QTuan
 */
public class DAOAdded {
    private Session session;
    public DAOAdded()
    {
        SessionFactory factory = NewHibernateUtil.getSessionFactory();
        this.session = factory.getCurrentSession();
    }
    public boolean setAdded(Added add)
    {
         try
        {
            
            session.getTransaction().begin();
            session.save(add);
            session.flush();
            session.getTransaction().commit();
            AddHistory addhistory = new AddHistory();
            addhistory.setAdded(add);
            addhistory.setAction("Tạo mới nhập kho");
            addhistory.setDtime(new Date());
            addhistory.setAccount(add.getPerAccount());
            DAOAddHistory daohis = new DAOAddHistory();
            daohis.setAddedHistory(addhistory);
            Object[] temp = add.getProducts().toArray();
            DAOProduct daoproduct = new DAOProduct();
            for(int i=0;i<temp.length;i++)
            {
                daoproduct.updateProduct((Product)temp[i]);
            }
            return true;
        }catch(Exception e)
        {
            if(session.getTransaction().isActive())
            {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return false;
    }
    public List<Added> getAllAdded()
    {
         try{
            session.getTransaction().begin();
            String sql = "from Added";
            Query query = session.createQuery(sql);
            List<Added> result = (List<Added>)query.list();
            session.flush();
            session.getTransaction().commit();
            return result;
        }catch(Exception e)
        {
            if(session.getTransaction().isActive())
            {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return null;
    }
    public Added getAddedById(int id)
    {
         try{
            session.getTransaction().begin();
            String sql = "from Added where id = ?";
            Query query = session.createQuery(sql);
            query.setInteger(0, id);
            Added result = (Added)query.uniqueResult();
            session.flush();
            session.getTransaction().commit();
            return result;
        }catch(Exception e)
        {
            if(session.getTransaction().isActive())
            {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return null;
    }
}
